﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.COM;
using System.Collections;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M009_Stocktaking : FormBase
    {
        #region 变量定义

        XGWMS.BLL.BLL_M009_STORAGE_INPUT bll_M009_STORAGE_INPUT = new XiaoGang.Soft.XGWMS.BLL.BLL_M009_STORAGE_INPUT();
        XGWMS.BLL.BLL_M007_GOODS bllGoods = new XiaoGang.Soft.XGWMS.BLL.BLL_M007_GOODS();
        XGWMS.BLL.BLL_M014BILLSCAN bllM014 = new XiaoGang.Soft.XGWMS.BLL.BLL_M014BILLSCAN();
        private string strGroupTitle = "@@@   提示：现存和实际相符可直接更新库存";
        MODEL.MOD_M007_GOODS SearchModel = new MODEL.MOD_M007_GOODS();
        string strStocktakingID = "暂无";//盘库编号
        string strStocktakingFlag = "0";//0=已盘+未盘 1=已盘 2=未盘
        int intFirstDisplayedScrollingRowIndex = 0;
        bool AddRowFlag = true;
        public string storageRoomID {get;set;}
        private const string dataName = "盘库";

        #endregion

        #region 方法

        public M009_Stocktaking()
        {
            InitializeComponent();
        }

        public M009_Stocktaking(string strBillID)
        {
            strStocktakingID = strBillID;
            InitializeComponent();
        }

        /// <summary>
        /// <p><b>方法名称</b>SetTabIndex</p>	
        /// <p><b>概要説明	:</b>画面フォーカスの遷移順の設定</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// </summary>
        private void SetTabIndex()
        {
            int intIndex = 0;
            grpTempBillInfo.TabIndex = intIndex; intIndex++;
            txtGOODS_NO.TabIndex = intIndex; intIndex++;
            cmbGOODS_GROUP.TabIndex = intIndex; intIndex++;
            txtGOODS_NM.TabIndex = intIndex; intIndex++;
            cmbSTOREROOM_ID.TabIndex = intIndex; intIndex++;
            ckbStocktakinged.TabIndex = intIndex; intIndex++;
            ckbNotStocktaking.TabIndex = intIndex; intIndex++;
            btnGoodsIDSearch.TabIndex = intIndex; intIndex++;
            groupBox1.TabIndex = intIndex; intIndex++;
            txtActualNuits.TabIndex = intIndex; intIndex++;
            btnUpdataNuits.TabIndex = intIndex; intIndex++;
            btnClose.TabIndex = intIndex; intIndex++;
        }

        /// <summary>
        /// <p><b>方法名称</b>SetEnable</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>概要説明  :</b>画面モードのセト</p>
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        private void SetEnable()
        {
            txtGOODS_NO.Enabled = true;
            txtGOODS_NM.Enabled = true;
            cmbGOODS_GROUP.Enabled = true;
            cmbSTOREROOM_ID.Enabled = true;
            ckbStocktakinged.Enabled = true;
            ckbNotStocktaking.Enabled = true;
            btnGoodsIDSearch.Enabled = true;
            txtActualNuits.Enabled = true;
            btnUpdataNuits.Enabled = true;
            btnClose.Enabled = true; 
        }

        /// <summary>
        /// <p><b>方法名称</b>ClearControl</p>	
        /// <p><b>概要説明	:</b>画面上の全項目をオールクリアする</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// </summary>
        private void ClearControl()
        {
            txtGOODS_NO.Text = "";
            txtGOODS_NM.Text = "";
            cmbGOODS_GROUP.SelectedIndex = 0;
            cmbSTOREROOM_ID.SelectedIndex = 0;
            ckbStocktakinged.Checked = false;
            ckbNotStocktaking.Checked = false;
            txtActualNuits.Text = "";
            txtNowUnits.Text = "";
        }

        /// <summary>
        /// <p><b>方法名称</b>InitComBox</p>	
        /// <p><b>概要説明	:</b>ComBoxの初期化する</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// </summary>
        private void InitComBox()
        {
            Method.CmbDataBound("STOREROOM_ID", cmbSTOREROOM_ID);           
            cmbSTOREROOM_ID.SelectedIndex = 0;
            Method.CmbDataBound("GOODS_GROUP_ID", cmbGOODS_GROUP);
            cmbSTOREROOM_ID.SelectedIndex = 0;    
        }

        /// <summary>
        /// <p><b>方法名称</b>InitialGridView</p>	
        /// <p><b>概要説明	:</b>Grid初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// </summary>
        private void InitialGridView()
        {
            grdmentTempBillInfo.ReadOnly = true;
            grdmentTempBillInfo.ImeMode = ImeMode.Disable;
            grdmentTempBillInfo.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grdmentTempBillInfo.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            grdmentTempBillInfo.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            grdmentTempBillInfo.ColumnHeadersHeight = 30;
            grdmentTempBillInfo.ColumnHeadersVisible = true;
            grdmentTempBillInfo.AllowUserToResizeColumns = true;
            grdmentTempBillInfo.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            grdmentTempBillInfo.Columns.Add("NO.", "NO.");
            grdmentTempBillInfo.Columns.Add("GOODS_ID", "商品ID");
            grdmentTempBillInfo.Columns.Add("GOODS_NO", "商品条码");
            grdmentTempBillInfo.Columns.Add("GOODS_NM", "商品名称");
            grdmentTempBillInfo.Columns.Add("GOODS_GROUP", "商品分类");
            grdmentTempBillInfo.Columns.Add("BUYING_PRICE", "进价");
            grdmentTempBillInfo.Columns.Add("SELLING_PRICE", "售价");
            grdmentTempBillInfo.Columns.Add("UNITS", "库存");
            grdmentTempBillInfo.Columns.Add("StocktakingUNITS", "盘库+-");
            grdmentTempBillInfo.Columns.Add("UPDATE_DATETIME", "盘库时间");
            grdmentTempBillInfo.Columns.Add("UPDATER_ID", "盘库员");
            grdmentTempBillInfo.Columns.Add("PROVIDER", "供应商");
            grdmentTempBillInfo.Columns.Add("MEMO", "备注");
            grdmentTempBillInfo.Columns.Add("BILL_ID", "盘库编号");

            grdmentTempBillInfo.Columns["NO."].Width = 50;
            grdmentTempBillInfo.Columns["NO."].Frozen = true;
            grdmentTempBillInfo.Columns["NO."].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentTempBillInfo.Columns["NO."].Resizable = DataGridViewTriState.False;
            grdmentTempBillInfo.Columns["NO."].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["GOODS_ID"].Width = 0;
            grdmentTempBillInfo.Columns["GOODS_ID"].Visible = false;
            grdmentTempBillInfo.Columns["GOODS_NO"].Width = 120;
            grdmentTempBillInfo.Columns["GOODS_NO"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentTempBillInfo.Columns["GOODS_NO"].Resizable = DataGridViewTriState.False;
            grdmentTempBillInfo.Columns["GOODS_NO"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["GOODS_NM"].Width = 240;
            grdmentTempBillInfo.Columns["GOODS_NM"].Frozen = true;
            //grdmentTempBillInfo.Columns["GOODS_NM"].Resizable = DataGridViewTriState.False;
            grdmentTempBillInfo.Columns["GOODS_NM"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["GOODS_GROUP"].Width = 90;
            //grdmentTempBillInfo.Columns["GOODS_GROUP"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentTempBillInfo.Columns["GOODS_GROUP"].Resizable = DataGridViewTriState.False;
            grdmentTempBillInfo.Columns["GOODS_GROUP"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["BUYING_PRICE"].Width = 60;
            grdmentTempBillInfo.Columns["BUYING_PRICE"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdmentTempBillInfo.Columns["BUYING_PRICE"].Resizable = DataGridViewTriState.False;
            grdmentTempBillInfo.Columns["BUYING_PRICE"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["BUYING_PRICE"].Visible = false;
            grdmentTempBillInfo.Columns["SELLING_PRICE"].Width = 80;
            grdmentTempBillInfo.Columns["SELLING_PRICE"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdmentTempBillInfo.Columns["SELLING_PRICE"].Resizable = DataGridViewTriState.False;
            grdmentTempBillInfo.Columns["SELLING_PRICE"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["SELLING_PRICE"].Visible = false;

            grdmentTempBillInfo.Columns["UPDATE_DATETIME"].Width = 150;
            grdmentTempBillInfo.Columns["UPDATE_DATETIME"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentTempBillInfo.Columns["UPDATE_DATETIME"].Resizable = DataGridViewTriState.False;
            grdmentTempBillInfo.Columns["UPDATE_DATETIME"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["UPDATER_ID"].Width = 80;
            grdmentTempBillInfo.Columns["UPDATER_ID"].Resizable = DataGridViewTriState.False;
            grdmentTempBillInfo.Columns["UPDATER_ID"].SortMode = DataGridViewColumnSortMode.NotSortable;

            grdmentTempBillInfo.Columns["UNITS"].Width = 60;
            grdmentTempBillInfo.Columns["UNITS"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdmentTempBillInfo.Columns["UNITS"].Resizable = DataGridViewTriState.False;
            grdmentTempBillInfo.Columns["UNITS"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["StocktakingUNITS"].Width = 60;
            grdmentTempBillInfo.Columns["StocktakingUNITS"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdmentTempBillInfo.Columns["StocktakingUNITS"].Resizable = DataGridViewTriState.False;
            grdmentTempBillInfo.Columns["StocktakingUNITS"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["PROVIDER"].Width = 80;
            grdmentTempBillInfo.Columns["PROVIDER"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdmentTempBillInfo.Columns["PROVIDER"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["MEMO"].Width = 126;
            grdmentTempBillInfo.Columns["MEMO"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentTempBillInfo.Columns["MEMO"].ReadOnly = false;
            grdmentTempBillInfo.Columns["BILL_ID"].Visible = false;

            grdmentTempBillInfo.AllowUserToAddRows = false;
            grdmentTempBillInfo.AllowUserToDeleteRows = false;
            grdmentTempBillInfo.AllowUserToResizeRows = false;
            grdmentTempBillInfo.RowHeadersVisible = false;
            grdmentTempBillInfo.RowTemplate.Height = 20;
            grdmentTempBillInfo.RowTemplate.ReadOnly = false;
            grdmentTempBillInfo.RowTemplate.Resizable = DataGridViewTriState.False;
        }

        /// <summary>
        /// <p><b>方法名称</b>InitProcess</p>	
        /// <p><b>概要説明	:</b>窗口初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// </summary>
        private void InitProcess()
        {
            if (GlobalVal.gloformStoreRoomSelect != null)
            {
                GlobalVal.gloformStoreRoomSelect.Hide();
            }
            SetTabIndex();
            SetEnable();
            InitialGridView();
            InitComBox();
            ClearControl();
            //绑定仓库
            if (!string.IsNullOrEmpty(storageRoomID))
            {
                DataTable tb = null;
                if(cmbSTOREROOM_ID.DataSource!=null)
                {
                    tb =(DataTable)cmbSTOREROOM_ID.DataSource;
                }
                for(int i=0;i<tb.Rows.Count;i++)
                {
                    if (tb.Rows[i]["STOREROOM_ID"].ToString().Trim() == storageRoomID)
                    {
                        cmbSTOREROOM_ID.SelectedIndex = i;
                        cmbSTOREROOM_ID.Enabled = false;
                        break;
                    }
                }
            }
            grpTempBillInfo.Text = "盘库编号:" + strStocktakingID;
        }

        /// <summary>	
        /// <p><b>方法名称</b>GetGoodsName</p>	
        /// <p><b>概要説明  :</b>获取商品的名称</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/16</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <param name="strValue">商品ID</param> 
        /// <returns type="string">商品名称</returns>
        protected bool GetGoodsName(string strValue)
        {
            XiaoGang.Soft.XGWMS.BLL.BLL_M007_GOODS bll_M007_GOODS =
                new XiaoGang.Soft.XGWMS.BLL.BLL_M007_GOODS();
            MODEL.MOD_M007_GOODS model =
                new XiaoGang.Soft.XGWMS.MODEL.MOD_M007_GOODS();
            model.Clear();
            model.GOODS_ID = strValue.Trim();

            ReturnValue rvReturnValue = bll_M007_GOODS.GetSingleData(model);

            if (rvReturnValue.IsSuccess == false)
            {
                Method.MessageShow(rvReturnValue.ErrorID,dataName);
                return false;
            }
            if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count == 0)
            {
                Method.MessageShow("W11008",dataName);
                return false;
            }

            txtGOODS_NM.Text =
                rvReturnValue.ResultDataSet.Tables[0].Rows[0]["GOODS_NM1"].ToString().Trim();
            return true;
        }

        /// <summary>
        /// <p><b>方法名称</b>CheckProcess</p>	
        /// <p><b>概要説明  :</b>画面のデータのＣＨＥＣＫ</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" param="strConstrolName">控件名称</param>	
        /// <returns type="Boolean">　True:成功   False:失敗 </returns>
        protected override bool CheckProcess(string strConstrolName)
        {
            switch (strConstrolName)
            {
                case "txtActualNuits":
                    if (Method.CheckIsNumber(txtActualNuits.Text.Trim(), txtActualNuits.MaxLength,true,true) == false)
                    {
                        Method.MessageShow(GlobalVal.glostrMsg, dataName);
                        txtActualNuits.Focus();
                        return false;
                    }         
                    break;
                case "txtGOODS_NM":
                    if (0 == txtGOODS_NM.Text.Trim().Length)
                    {
                        Method.MessageShow("W11E02", "商品名称");//[@]不能为空！
                        txtGOODS_NM.Focus();
                        return false;
                    }
                    if (Method.CheckIsIncludeSpecailChar(txtGOODS_NM.Text.Trim()) == false)
                    {
                        Method.MessageShow(GlobalVal.glostrMsg,dataName);
                        txtGOODS_NM.Focus();
                        return false;
                    }
                    if (txtGOODS_NM.Text.Trim().Length > txtGOODS_NM.MaxLength)
                    {
                        Method.MessageShow("W11007", dataName);
                        txtGOODS_NM.Focus();
                        return false;
                    }
                    break;                
            }

            return true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>CheckProcessLoop</p>	
        /// <p><b>概要説明  :</b>画面のデータのＣＨＥＣＫ</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" param="strConstrolName">控件名称</param>	
        /// <returns type="Boolean">　True:成功   False:失敗 </returns>
        private bool CheckProcessLoop(enumOperation emOperation)
        {
            switch (emOperation)
            {   
                case enumOperation.Add:
                    if (CheckProcess("txtActualNuits") == false) return false;
                    break;
            }
            return true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>NumPriceFormat</p>	
        /// <p><b>概要説明  :</b>num and price date's format</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" param="strValue">没有格式化的字符串</param>	
        /// <returns type="string">格式化后的字符串</returns>
        protected string NumPriceFormat(string strValue)
        {
            string strTemp = strValue;
            if (strValue.Length > 8)
            {
                return strValue;
            }
            if (strValue.Length > 6)
            {
                strTemp = strValue.Substring(0, strValue.Length - 6) + ","
                    + strValue.Substring((strValue.Substring(0, strValue.Length - 6)).Length, 3)
                    + "," + strValue.Substring(strValue.Length - 3, 3);

            }
            else if (strValue.Length > 3)
            {
                strTemp = strValue.Substring(0, strValue.Length - 3) + ","
                + strValue.Substring((strValue.Substring(0, strValue.Length - 3)).Length, 3);
            }
            return strTemp;
        }

        /// <summary>	
        /// <p><b>方法名称</b>DateFormat</p>	
        /// <p><b>概要説明  :</b>date data format</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/16</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <param name="strValue">没有格式化的字符串</param> 
        /// <returns type="string">格式化后的字符串</returns>
        protected string DateFormat(string strValue)
        {
            if (strValue.Length < 8)
            {
                return strValue;
            }
            return strValue.Substring(0, 4) + "/" +
                strValue.Substring(4, 2) + "/" +
                strValue.Substring(6, 2);
        }

        /// <summary>	
        /// <p><b>方法名称</b>AddRowToGird</p>	
        /// <p><b>概要説明  :</b>a new row add to grid</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        private void AddRowToGird()
        {
            AddRowFlag = false;
            grdmentTempBillInfo.Rows.Clear();
            ReturnValue returnV = bllM014.GetStocktakingGoods(strStocktakingID, strStocktakingFlag, SearchModel);
            if (returnV.IsSuccess == false)
            {
                Method.MessageShow(returnV.ErrorID, dataName);
                AddRowFlag = true;
                return;
            }
            if (0 == returnV.ResultDataSet.Tables[0].Rows.Count)
            {
                grpData.Text = "没有找到符合查询条件的商品";
                AddRowFlag = true;
                return;
            }
            foreach (DataRow row in returnV.ResultDataSet.Tables[0].Rows)
            {
                grdmentTempBillInfo.Rows.Insert(grdmentTempBillInfo.Rows.Count,
                    (grdmentTempBillInfo.Rows.Count + 1).ToString(),//NO.
                    row["GOODS_ID"].ToString().Trim(),//商品编码
                    row["GOODS_NO"].ToString().Trim(),//商品条码
                    row["GOODS_NM1"].ToString().Trim(),//商品名称
                    row["GOODS_GROUP_NM"].ToString().Trim(),//商品分类
                    row["BUYING_PRICE"].ToString().Trim(),//进价
                    row["SELLING_PRICE"].ToString().Trim(),//售价
                    row["UNITS"].ToString().Trim() == "" ? "0" : row["UNITS"].ToString().Trim(),//库存
                    row["StocktakingUNITS"].ToString().Trim(),//盘库+-
                    DateFormat(row["UPDATE_DATETIME"].ToString().Trim()),//盘库日期
                    row["USER_NAME"].ToString().Trim(),//盘库员
                    row["PROVIDER"].ToString().Trim(),//供应商
                    row["MEMO"].ToString().Trim(),//备注
                    row["BILL_ID"].ToString().Trim()//盘库编号
                    );
                grpData.Text = "共" + grdmentTempBillInfo.Rows.Count.ToString() + "条记录";
                //if (grdmentTempBillInfo.Rows.Count > 16)
                //{
                //    grdmentTempBillInfo.Columns["GOODS_NM"].Width = 223;
                //}
                //else
                //{
                //    grdmentTempBillInfo.Columns["GOODS_NM"].Width = 240;
                //}
            }
            SetGridBackColor();
            AddRowFlag = true;
        }

        /// <summary>
        /// 商品入库
        /// </summary>
        /// <param name="modelSTORAGE"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public bool InPutStorage(List<MODEL.MOD_M009_STORAGE_INPUT> modelSTORAGE, out string strMessage)
        {

            #region 创建新的货单号

            string strReturn = string.Empty;
            MODEL.MOD_M010_BILL modelBill = new XiaoGang.Soft.XGWMS.MODEL.MOD_M010_BILL();
            modelBill.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID.Trim();
            modelBill.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD.Trim();
            string strBillID = bll_M009_STORAGE_INPUT.GetBillID(modelBill);
            if (strBillID.Trim().Length != 12)
            {
                strMessage = strBillID.Trim();
                return false;
            }

            #endregion

            #region INSERT INTO BILL

            modelBill.TRANS_STATE = "1";
            modelBill.BILL_ID = strBillID;
            modelBill.IN_OUT = "0";
            //modelBill.BUSINESS_TIME = txtBUSINESS_TIME.Text.Trim().Replace("/", "");
            modelBill.MEMO = "正常入库";
            modelBill.USER_NO = "";
            strReturn = bll_M009_STORAGE_INPUT.InsertProcess(modelBill);
            if (strReturn.Trim() != "0")
            {
                strMessage = strReturn.Trim();
                return false;
            }

            #endregion

            #region InTo Storage

            for (int i = 0; i < modelSTORAGE.Count; i++)
            {
                modelSTORAGE[i].BILL_ID = strBillID;
                strReturn = bll_M009_STORAGE_INPUT.InsertProcess(modelSTORAGE[i]);
                //Message
                if (strReturn.Trim() != "0")
                {
                    strMessage = strReturn.Trim();
                    return false;
                }
            }

            #endregion

            strMessage = "";
            return true;
        }

        /// <summary>
        /// 商品检索
        /// </summary>
        private void SearchGoods()
        {
            SearchModel.GOODS_NO = txtGOODS_NO.Text.Trim();
            SearchModel.GOODS_NM1 = txtGOODS_NM.Text.Trim();
            SearchModel.GOODS_GROUP = cmbGOODS_GROUP.SelectedValue.ToString();
            SearchModel.StoreRoom_ID = cmbSTOREROOM_ID.SelectedValue.ToString();
            strStocktakingFlag = "0";//0=已盘+未盘 1=已盘 2=未盘
            if(ckbNotStocktaking.Checked && !ckbStocktakinged.Checked)
            {
                strStocktakingFlag = "2";//未盘
            }
            else if(!ckbNotStocktaking.Checked && ckbStocktakinged.Checked)
            {
                strStocktakingFlag = "1";//已盘
            }
            AddRowToGird();
        }

        private void SetGroupTitle()
        {

            if (AddRowFlag)
            {
                intFirstDisplayedScrollingRowIndex = grdmentTempBillInfo.FirstDisplayedScrollingRowIndex;
            }
            string strTemp = "商品编号:[" + grdmentTempBillInfo.CurrentRow.Cells["GOODS_ID"].Value.ToString()
                 + "]  商品名称:[" + grdmentTempBillInfo.CurrentRow.Cells["GOODS_NM"].Value.ToString() + "]";
            groupBox1.Text = strGroupTitle.Replace("@@@", strTemp);
            lbGoodsID.Text = grdmentTempBillInfo.CurrentRow.Cells["GOODS_ID"].Value.ToString();
            txtNowUnits.Text = grdmentTempBillInfo.CurrentRow.Cells["UNITS"].Value.ToString().Trim() == "" ? "0" : grdmentTempBillInfo.CurrentRow.Cells["UNITS"].Value.ToString().Trim();
            txtActualNuits.Text = "";
        }

        private void SetGridBackColor()
        {
            for (int i = 0; i < grdmentTempBillInfo.Rows.Count; i++)
            {
                if (grdmentTempBillInfo.Rows[i].Cells["BILL_ID"].Value.ToString().Trim().Length > 0)
                {
                    grdmentTempBillInfo.Rows[i].DefaultCellStyle.BackColor = Color.DarkOrange;
                }
            }
        }

        #endregion

        #region 事件

        private void M009_STORAGE_INPUT_Load(object sender, EventArgs e)
        {
            InitProcess();
            SearchGoods();
            txtGOODS_NM.Focus();
            GlobalVal.SplashObj.Dispose();
        }
      
        private void grdmentTempBillInfo_ButtonClick(object sender, DataGridViewCellEventArgs e)
        {
            SetGroupTitle();
        }      
       
        protected override void AllTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            base.AllTextBox_KeyDown(sender, e);
        }

        private void AllTextBox_KeyPrecess(object sender, KeyPressEventArgs e)
        {
            string strValue = ((TextBox)sender).Text.Trim();
            switch (((TextBox)sender).Name)
            {
                case "txtGOODS_ID":
                    FormBase.UKeyPress(e, InputType.Num);
                    break;
                case "txtBUSINESS_TIME":
                    FormBase.UKeyPress(e, InputType.Date);
                    break;
                case "txtActualNuits":
                    FormBase.UKeyPress(e, InputType.Num);
                    break;
            }
        }

        private void AllButton_Click(object sender, EventArgs e)
        {
            if (Cursor.Current == Cursors.WaitCursor)
            {
                return;
            }
            Cursor.Current = Cursors.WaitCursor;

            string strButtonName = ((Button)sender).Name;

            switch (strButtonName)
            {
                #region btnGoodsIDSearch

                case "btnGoodsIDSearch":
                    {
                        SearchGoods();
                    }
                    break;

                #endregion

                #region btnUpdataNuits

                case "btnUpdataNuits":
                    if (CheckProcessLoop(enumOperation.Add) == false)
                    {
                        Cursor.Current = Cursors.Default;
                        return;
                    }
                    if (0 == grdmentTempBillInfo.Rows.Count)
                    {
                        Method.MessageShow("W11047",dataName);//没有商品！
                        Cursor.Current = Cursors.Default;
                        return;
                    }
                    if (lbGoodsID.Text.Trim().Length<1)
                    {
                        Method.MessageShow("I11003",dataName);//请首先选择商品！
                        Cursor.Current = Cursors.Default;
                        return;
                    }
                    ReturnValue retuenValue = new ReturnValue(false);
                    
                    //添加、更新流水表
                    //如果存在==>>更新
                    //如果不存在==>>添加

                    if (string.IsNullOrEmpty(strStocktakingID))
                    {
                        //创建新的货单号
                        string strReturn = string.Empty;
                        MODEL.MOD_M010_BILL modelBill = new XiaoGang.Soft.XGWMS.MODEL.MOD_M010_BILL();
                        modelBill.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID.Trim();
                        modelBill.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD.Trim();
                        string strBillID = bll_M009_STORAGE_INPUT.GetBillID(modelBill);
                        if (strBillID.Trim().Length != 12)
                        {
                            Method.MessageShow(strBillID.Trim(), dataName);
                            this.Close();
                            return;
                        }
                        //添加新盘库单
                        modelBill.TRANS_STATE = "1";
                        modelBill.BILL_ID = strBillID;
                        modelBill.IN_OUT = "2";
                        modelBill.BUSINESS_TIME = System.DateTime.Now.ToString("yyyyMMdd");
                        modelBill.MEMO = "盘库";
                        modelBill.USER_NO = "";
                        strReturn = bll_M009_STORAGE_INPUT.InsertProcess(modelBill);
                        if (strReturn.Trim() != "0")
                        {
                            Method.MessageShow(strReturn.Trim(), dataName);
                            this.Close();
                            return;
                        }
                        strStocktakingID = strBillID;
                        grpTempBillInfo.Text = "盘库编号:" + strStocktakingID;
                    }
                    //更新库存
                    MODEL.MOD_M009_STORAGE_INPUT modelStorage = new XiaoGang.Soft.XGWMS.MODEL.MOD_M009_STORAGE_INPUT();
                    modelStorage.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID.Trim();
                    modelStorage.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD.Trim();
                    modelStorage.STORAGE_ID = "";//采番
                    modelStorage.BILL_ID = strStocktakingID;
                    modelStorage.STOREROOM_ID = cmbSTOREROOM_ID.SelectedValue.ToString();
                    modelStorage.GOODS_ID = lbGoodsID.Text;
                    if (txtActualNuits.Text.Trim().Length < 1)
                    {
                        modelStorage.UNITS = 0;
                    }
                    else
                    {
                        modelStorage.UNITS = Int32.Parse(txtActualNuits.Text.Trim()) - Int32.Parse(txtNowUnits.Text.Trim());
                    }
                    modelStorage.PRODUCE_DATE = "";
                    modelStorage.OVER_PRODUCE_DATE = "";
                    modelStorage.BUYING_PRICE = 0m;
                    modelStorage.SELLING_PRICE = 0m;                   
                    modelStorage.RED_BLACK = "0";
                    modelStorage.MEMO = "盘库流水";
                    retuenValue = bll_M009_STORAGE_INPUT.AddUpdateStorage(modelStorage);
                    if (!retuenValue.IsSuccess)
                    {
                        Method.MessageShow(retuenValue.ErrorID, dataName);
                        return;
                    }

                    //更新现在商品数量********************************                    
                    if (txtNowUnits.Text.Trim() == txtActualNuits.Text.Trim() || txtActualNuits.Text.Trim().Length < 1)
                    {
                        //如果数量相同或要更新的数量为空==>>不更新
                    }
                    else
                    {
                        //如果数量不同相同==>>更新
                        XGWMS.MODEL.MOD_M007_GOODS modGoods = new XiaoGang.Soft.XGWMS.MODEL.MOD_M007_GOODS();
                        modGoods.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID.Trim();
                        modGoods.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD.Trim();
                        modGoods.UNITS = Int32.Parse(txtActualNuits.Text.Trim());
                        modGoods.GOODS_ID = lbGoodsID.Text;
                        retuenValue = bllGoods.UpdateGoodsUnits(modGoods);
                        if (!retuenValue.IsSuccess)
                        {
                            Method.MessageShow(retuenValue.ErrorID, dataName);
                            return;
                        }
                    }

                    AddRowToGird();
                    //定位滚动条位置
                    grdmentTempBillInfo.FirstDisplayedScrollingRowIndex = intFirstDisplayedScrollingRowIndex;
                    SetGroupTitle();
                    break;

                #endregion 入库

                #region btnClose

                case "btnClose":
                    {
                        if (this.Cursor == Cursors.WaitCursor)
                        {
                            return;
                        }
                        this.Close();
                    }
                    break;

                #endregion
            }
            Cursor.Current = Cursors.Default;
        }

        private void cmbSTOREROOM_ID_GotFocus(object sender, EventArgs e)
        {            
            cmbSTOREROOM_ID.SelectAll();
        }        

        private void dtpBUSINESS_TIME_CloseUp(object sender, EventArgs e)
        {
            //txtBUSINESS_TIME.Text = Method.TimeFormat( dtpBUSINESS_TIME.Value.ToString("yyyyMMdd"));
        }

        private void dtpPRODUCE_DATE_KeyDown(object sender, KeyEventArgs e)
        {
            base.AllTextBox_KeyDown(sender, e);
        }

        private void dtpOVER_PRODUCE_DATE_KeyDown(object sender, KeyEventArgs e)
        {
            base.AllTextBox_KeyDown(sender, e);
        }        

        private bool CheckUnitsBuyingPriceDate(string strUnits,string strBuyingPrice,string strProduceDate,string strOverProduceDate,out string strMessage)
        {
            if (strUnits != "")//Check商品数量
            {   
                int UNITS = 0;
                if (!Method.CheckIsOnlyNum(strUnits))
                {
                    strMessage="W11039";//商品数量必须为正整数！
                    return false;
                }
                UNITS = Convert.ToInt32(strUnits);
                if (UNITS <= 0)
                {
                    strMessage="W11039";//商品数量必须为正整数！
                    return false;
                }
            }
            else
            {
                strMessage="W11046";//商品数量不能为空！
                return false;
            }

            if (strBuyingPrice != "")//Check 进价
            {
                if (!Method.CheckIsNumerc(strBuyingPrice))
                {
                    strMessage = "W11041";//进价必须为数字！
                    return false;
                }
                decimal BUYING_PRICE = decimal.Parse(strBuyingPrice);
                if (BUYING_PRICE <= 0)
                {
                    strMessage = "W11042";//进价必须大于0！
                    return false;
                }
            }

            if (strProduceDate != "")//Check 生产日期
            {
                if (!Method.CheckIsDate(strProduceDate))
                {
                    strMessage = "W11043";//生产日期不是合理的日期格式！
                    return false;
                }
            }

            if (strOverProduceDate != "")//Check 过期日期
            {
                if (!Method.CheckIsDate(strOverProduceDate))
                {
                    strMessage="W11044";//过期日期不是合理的日期格式！
                    return false;
                }
            }
            strMessage = "";
            return true;
        }

        private void M009_STORAGE_INPUT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString().Trim() == "")
            {
                txtGOODS_NO.Text = "";
                txtGOODS_NO.Focus();
            }
        }        

        private void grdmentTempBillInfo_SelectionChanged(object sender, EventArgs e)
        {
            SetGroupTitle();
        }

        private void M009_Stocktaking_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    if (this.Cursor == Cursors.WaitCursor)
                    {
                        return;
                    }
                    this.Close();
                    break;
            }
        }

        #endregion

        private void M009_Stocktaking_FormClosing(object sender, FormClosingEventArgs e)
        {
            GlobalVal.globFinishStocktaking = true;
        }
    }
}